<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/5/21 0021
 * Time: 下午 4:21
 */
namespace app\model;
class Job extends  \think\Model{

    public $table="job";

    function add_job($data){
        $data['created_at']=cur_time();
        $user_id=$data['user_id'];
        $UserCompany=new UserCompany();
        $user_company_id=$UserCompany->get_id_by_user_id($user_id);
        $data['user_company_id']=$user_company_id;

        return $this->insert($data,false,true);
    }

    /**
     * 根据用户ID，返回他的发布的职位列表
     * @param int $page
     * @param array $params
     * @return mixed
     */
    function get_job_list_by_user_id($page=1,$user_id,$params=[]){
        $offset=($page-1)*10;
        $o=$this->limit($offset,10)->where(['user_id'=>$user_id])->select();
        $arrs=object2array($o);
        foreach($arrs as $k=>&$v){
            $this->toTypeToName($v);
        }
        return $arrs;
    }


    function toTypeToName(&$v){
        $v['job_type_name']=config("web.job_type")[$v['job_type']];
        $v['treatment_type_name']=config('web.treatment_type')[$v['treatment_type']];
        $v['salary_range_type_name']=config('web.salary_range_type')[$v['salary_range_type']];
        $v['department_type_name']=config('web.department_type')[$v['department_type']];
        $v['work_experience_type_name']=config('web.work_experience_type')[$v['work_experience_type']];
        $v['edu_type_name']=config('web.edu_type')[$v['edu_type']];
        $v['age_type_name']=config('web.age_type')[$v['age_type']];
        $v['highlight_type_name']=config("web.highlight_type")[$v['highlight_type']];
        $v['sex_type_name']=config("web.sex_type")[$v['sex_type']];

        $Places=new Places();
        if($v['area_id']!=false){
            $v['city_name']=$Places->get_name_by_id($v['area_id']);
        }
        if($v['province_id']!=false){
            $v['province_name']=$Places->get_name_by_id($v['province_id']);
        }


        return $v;
    }

    /**
     * 根据主键，返回工作的详情
     * @param $id
     * @return array|mixed
     */
    function get_job_info_by_id($id){
        $o=$this->where(['id'=>$id])->find();
        $arr=object2array($o);
        if($arr!=false){
            $this->toTypeToName($arr);
            $UserCompany=new UserCompany();
            $arr['company']=$UserCompany->get_user_company($arr['user_id']);
            return $arr;
        }else{
            return [];
        }
    }

    /**
     * 返回所有的工作列表
     * @param int $page
     * @param array $params
     * @return mixed
     */
    function get_all_job($page=1,$params=[]){

        if(!if_null($params['k'])){
            $o=$this->where("job_name",'like',"%{$params['k']}%");
        }else{
            $o=$this;
        }

        $offset=($page-1)*10;
        $o=$o->limit($offset,10)->select();
        $arrs=object2array($o);
        $UserCompany=new UserCompany();
        foreach($arrs as $k=>&$v){
            $this->toTypeToName($v);
            $arrs[$k]['company']=$UserCompany->get_user_company($v['user_id']);
        }
        return $arrs;
    }

    /**
     * 根据ID返回ID指定的信息是否存在
     * @param $id
     * @return bool
     */
    function if_exit_by_id($id){
        $o=$this->where(['id'=>$id])->column('id');
        if($o[0]==true){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 根据公司对应的用户ID,返回他发布的所有工作列表
     * @param $user_id
     * @return mixed
     */
    function get_all_job_list_by_user_id($user_id){

        $o=$this->where(['user_id'=>$user_id])->select();
        $arrs=object2array($o);
        foreach($arrs as $k=>&$v){
            $this->toTypeToName($v);
        }
        return $arrs;
    }


    function get_all_job_key($user_id){
        $o=$this->where(['user_id'=>$user_id])->field("id,job_name")->select();
        //var_dump($this->getLastSql());
        $arrs=object2array($o);
        return $arrs;
    }




}